package com.example.elemboot.Service.impl;

import com.example.elemboot.Dao.IUser;
import com.example.elemboot.Entity.User;
import com.example.elemboot.Service.IUserService;
import com.example.elemboot.Utils.TokenUtils;
import com.example.elemboot.Vo.UserVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {
    @Resource
    private IUser userDao;
    @Override
    public UserVO login(String phone, String pwd) {
        UserVO vo = new UserVO();
        try {
            User user = userDao.findUserByUserPhoneAndPassword(phone, pwd);
            if (user == null){
                return null;
            }
            String token = TokenUtils.createToken(user.getUserPhone(), user.getPassword());
            user.setToken(token);
            BeanUtils.copyProperties(user,vo);
            return vo;
        }catch (Exception e){
            return vo;
        }
    }

    @Override
    public boolean register(User user) {
        if (userDao.existsUserByUserPhone(user.getUserPhone())){
            return false;
        }
        userDao.save(user);
        return true;
    }
}
